package bom.example.common.quarzt.controller;

import bom.example.common.quarzt.domain.SysJob;
import bom.example.common.quarzt.service.SysJobService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/8 18:15
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"定时任务模块"})
@RequestMapping("job")
public class SysJobController {

    @Autowired
    SysJobService sysJobService;

    @PostMapping("getSysJob")
    public JSONObject sysJobController(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                       @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                       @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        LambdaQueryWrapper<SysJob> sysJobLambdaQueryWrapper= Wrappers.lambdaQuery();
        if(!StringUtils.isEmpty(keyword)){
            sysJobLambdaQueryWrapper.like(SysJob::getJobname,keyword);
            sysJobLambdaQueryWrapper.or().like(SysJob::getJobgroup,keyword);
            sysJobLambdaQueryWrapper.or().like(SysJob::getClazz,keyword);
        }
        sysJobLambdaQueryWrapper.orderByDesc(SysJob::getCreateTime);

        IPage<SysJob> sysJobIPage=sysJobService.page(new Page(pageNum,pageSize),sysJobLambdaQueryWrapper);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("total",sysJobIPage.getTotal());
        jsonObject.put("rows",sysJobIPage.getRecords());
        return jsonObject;
    }

    @PostMapping("addMethodJob")
    @ApiOperation("添加job反射调用任务")
    public JSONObject addMethodJobController(@ApiParam("反射路径")@RequestParam("methodClazz") String methodClazz,
                                             @ApiParam("反射方法名")@RequestParam("methodName")String methodName,
                                             @ApiParam("方法参数")@RequestParam(value = "params",required = false)String params,
                                             @ApiParam("方法分组名")@RequestParam("groupName")String groupName,
                                             @ApiParam("方法任务名")@RequestParam("jobName")String jobName,
                                             @ApiParam("方法执行分钟")@RequestParam("time")String time){
        JSONObject obj=new JSONObject();
        Class<?> cls=null;
        try {
            cls=Class.forName(methodClazz);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(cls==null){
            obj.put("msg","方法并不存在");
            return obj;
        }
        SysJob sysJob=new SysJob();
        sysJob.setMethodName(methodName);
        sysJob.setMethodClazz(methodClazz);
        sysJob.setClazz("exam.quarzt.config.MethodJob");
        sysJob.setStatus(0);
        sysJob.setJobgroup(groupName);
        sysJob.setJobname(jobName);
        sysJob.setTypeCode(1); //类型编码 1 反射 2 rest 3 定时调用
        sysJob.setType("方法调用");
        JSONArray jsonArray=new JSONArray();
        if(StringUtils.isEmpty(params)){
            jsonArray.add(params);
        }
        sysJob.setParam(jsonArray.toJSONString());
        String cron="0 */"+time+" * * * ?";
        sysJob.setCron(cron);
        sysJobService.save(sysJob);
        obj.put("msg","方法添加成功");
        return obj;
    }
}
